package com.koicarp.agent.common.tools;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.Arrays;

import com.koicarp.agent.common.constant.CommonConstant;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;




/**
 * 
 * @ClassName:  FileUtil   
 * @Description: 文件工具类 
 * @author: liutao
 * @date:   2022年12月4日 下午4:47:52    
 * @Copyright:
 */
public class FileUtil {
	
    private static Logger logger = LoggerFactory.getLogger(FileUtil.class);
    
    /**
     * @param parent 已创建的父目录
     * @param filePath 子目录/文件子路径
     * @return  File
     */
    public static File createFile(File parent, String filePath) {
        File file = new File(parent, filePath);
        return file;
    }

    /**
     * 创建路径
     * @param parent
     * @param filePath
     * @return
     */
    public static File createDir(File parent, String filePath) {
        String[] subPath = filePath.split("\\" + CommonConstant.FILE_SEPARATOR);
        File file = null;
        for (String path : subPath) {
            file = new File(parent, path);
            if (!file.exists()) {
                file.mkdir();
            }
            parent = file;
        }
        return file;
    }

    public static void delete(String path) {
        File file = new File(path);
        FileUtil.delete(file);
    }
    
    /**
     * 删除文件
     * @param file
     */
    public static void delete(File file) {
        if (file.isDirectory()) {
            File[] files = file.listFiles();
            Arrays.stream(files).forEach(f -> delete(f));
            file.delete();
            return;
        }
        file.delete();
    }

    public static void write(String fileUrl, byte[] data) {
    	System.out.println("1:  "+ fileUrl );
		try (FileOutputStream fos = new FileOutputStream(fileUrl)) {
			fos.write(data);
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
    
    public static void write(String fileUrl, InputStream inp) throws FileNotFoundException, IOException {
    	try (OutputStream outputStream = new FileOutputStream(fileUrl)) {
            byte[] buffer = new byte[1024];
            int length;
            while ((length = inp.read(buffer)) != -1) {
                outputStream.write(buffer, 0, length);
            }
        }
	}
    /**
     * 写出文件到流中
     * 
     * @param source
     * @param output
     */
    public static void writeToOutput(File source, OutputStream output) {
        byte[] buf = new byte[1024];
        int len;
        try(BufferedInputStream fis = new BufferedInputStream(new FileInputStream(source))) {
            while((len = fis.read(buf)) != -1){
                output.write(buf, 0 , len);
            }
        }catch (IOException e) {
            throw new RuntimeException("write to output error , destFile path:" + source.getAbsolutePath(), e);
        }
    }

    /**
     * 复制文件
     * 
     * @param source
     * @param target
     * @throws IOException
     */
    public static void copyFile(File source, File target) throws IOException {
        byte[] buf = new byte[1024];
        FileInputStream in = new FileInputStream(source);
        FileOutputStream out = new FileOutputStream(target);
        while (in.read(buf) != -1) {
            out.write(buf);
        }
        in.close();
        out.close();
    }

}
